<template>
  <div id="app">
    <a-input  placeholder="请输入任务" class="my_ipt" :value="inputValue" @change="handlerinputchange"/>
    <a-button type="primary" @click="additemtolist">添加事项</a-button>

    <!-- 列表标签上:dataSource="list" 绑定数据源 -->
    <a-list bordered class="dt_list" :dataSource="infolist">
      <!-- 每个Item项 slot="renderItem" slot-scope="item" -->
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="(e)=>{cbstatuschange(e, item.id)}">{{ item.info }}</a-checkbox>
        <!-- 删除链接 -->
        <a href="javascript:;" class="a-list-item-a" @click="removeitembyid(item.id)">删除</a>
      </a-list-item>
      <!-- footer区域 -->
      <div class="footer">
        <!-- 未完成任务的个数 -->
        <span>{{undonelength}} 条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewkey === 'all' ? 'primary' : 'default'" @click="changeviewlist('all')">全部</a-button>
          <a-button :type="viewkey === 'undone' ? 'primary' : 'default'" @click="changeviewlist('undone')">未完成</a-button>
          <a-button :type="viewkey === 'done' ? 'primary' : 'default'" @click="changeviewlist('done')">已完成</a-button>
        </a-button-group>
        <a href="javascript:;" @click="clean">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'MyApp',
  created() {
    this.$store.dispatch('getList')
  },
  data () {
    return {
    }
  },
  methods: {
    // 监听文本框内容变化
    handlerinputchange(e) {
      this.$store.commit('setinputvalue', e.target.value)
    },
    // 向列表中新增Item项
    additemtolist() {
      if (this.inputValue.trim().length <= 0) return this.$message.warning('文本框内容不能为空！')
      this.$store.commit('additem')
    },
    // 删除任务项
    removeitembyid(id) {
      this.$store.commit('removeitem', id)
    },
    // 改变复选框状态
    cbstatuschange(e, id) {
      this.$store.commit('statechange', { id: id, state: e.target.checked })
    },
    // 清除已完成
    clean() {
      this.$store.commit('cleardone')
    },
    changeviewlist(key) {
      this.$store.commit('changeview', key)
    }
  },
  computed: {
    // 注意mapState是一个方法
    ...mapState(['inputValue', 'viewkey']),
    ...mapGetters(['undonelength', 'infolist'])
  }
}
</script>

<style lang="less" scoped>
  #app {
    padding: 10px;
  }
  .my_ipt {
    width: 500px;
    margin-right: 10px;
  }
  .dt_list {
    width: 500px;
    margin-top: 10px;
  }
  .footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
  }
</style>
